import StringUtils from "@/utils/StringUtils";

export default class AppEnvironments {
  baseURL: string;
  ossURL: string;
  name: string;

  constructor(name: string, baseURL: string, ossURL: string) {
    this.baseURL = baseURL;
    this.name = name;
    this.ossURL = ossURL;
  }

  static test = new AppEnvironments(
    '测试环境',
    'https://test-monitoring.tsun-ess.com/api/',
    'https://test-monitoring.tsun-ess.com/api/'
  );
  static production = new AppEnvironments(
    '正式环境',
    'http://47.99.86.62/api/',
    'https://tsmnt.oss-eu-central-1.aliyuncs.com/'
  );

  static readonly allEnvironments = [
    AppEnvironments.test,
    AppEnvironments.production,
  ];

  static current: AppEnvironments = AppEnvironments.production;

  static parse(json: string | null | undefined): AppEnvironments | null {
    if (StringUtils.isEmpty(json)) {
      return null;
    }
    let obj = JSON.parse(json!);
    let baseURL = obj?.baseURL ?? '';
    let ossURL = obj?.ossURL ?? '';
    let name = obj?.name ?? '';
    return new AppEnvironments(name, baseURL, ossURL);
  }

  toJSON(): any {
    return {
      baseURL: this.baseURL,
      ossURL: this.ossURL,
      name: this.name,
    };
  }
}
